package com.macro.mall.service.impl;

import com.macro.mall.common.api.CommonResult;
import com.macro.mall.dao.OpenDoorRecordDao;
import com.macro.mall.dto.OpenDoorRecord;
import com.macro.mall.dto.OpenDoorRecordExample;
import com.macro.mall.dto.OpenDoorRecordRequest;
import com.macro.mall.service.OpenDoorRecordService;
import com.macro.mall.util.example.ExampleUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author HuangZiy
 * @version 8.0
 * @date 2021/4/26 22:43
 */
@Service
public class OpenDoorRecordServiceImpl implements OpenDoorRecordService {
    @Autowired
    private OpenDoorRecordDao openDoorRecordDao;

    @Override
    public CommonResult<List<OpenDoorRecord>> getOpenDoorRecord(OpenDoorRecordRequest request) {
        OpenDoorRecordExample openDoorRecordExample = ExampleUtil.getExampleByAnnotation(request, OpenDoorRecordExample.class);
        List<OpenDoorRecord> openDoorRecord = openDoorRecordDao.selectByExample(openDoorRecordExample);
        if (CollectionUtils.isEmpty(openDoorRecord)) {
            return CommonResult.success(null);
        }
        return CommonResult.success(openDoorRecord);
    }

    @Override
    public CommonResult<String> addOpenDoorRecord(OpenDoorRecordRequest request) {
        OpenDoorRecord openDoorRecord = new OpenDoorRecord();
        BeanUtils.copyProperties(request, openDoorRecord);
        int i = openDoorRecordDao.insertSelective(openDoorRecord);
        if (i <= 0) {
            return CommonResult.failed("插入失败");
        }
        return CommonResult.success("插入成功");
    }

    @Override
    public CommonResult<String> updateOpenDoorRecord(OpenDoorRecordRequest request) {
        OpenDoorRecord openDoorRecord = new OpenDoorRecord();
        BeanUtils.copyProperties(request, openDoorRecord);
        int i = openDoorRecordDao.updateByPrimaryKeySelective(openDoorRecord);
        if (i <= 0) {
            return CommonResult.failed("修改失败");
        }
        return CommonResult.success("修改成功");
    }

    @Override
    public CommonResult<String> deleteOpenDoorRecord(Integer id) {
        int i = openDoorRecordDao.deleteByPrimaryKey(id);
        if (i <= 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success("删除成功");
    }
}
